package top.infopub.mgr.cap.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.InformationSubmitApprovalDto;
import top.infopub.mgr.cap.dao.FundingApprovalDao;
import top.infopub.mgr.cap.dao.InformationSubmitApprovalDao;
import top.infopub.mgr.cap.service.InformationSubmitApprovalService;
import top.infopub.mgr.common.filter.SessionTimeOutFilter;
import top.infopub.model.cap.*;
import top.infopub.model.flow.ProcessNode;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Warking
 * @Date 2021/5/22 15:35
 * @Version 1.0 资料报审
 */
@Service
public class InformationSubmitApprovalServiceImpl implements InformationSubmitApprovalService {

    @Autowired
    private InformationSubmitApprovalDao informationSubmitApprovalDao;
    @Autowired
    private FundingApprovalDao fundingApprovalDao;

    @Override
    /*资料报审列表接口*/
    public Page<InformationSubmitApproval> selectPage(Page<InformationSubmitApproval> page, InformationSubmitApproval informationSubmitApproval) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return informationSubmitApprovalDao.selectPage(informationSubmitApproval);
    }

    /*资料报审详情跳转查询主阶段表的数据*/
    @Override
    public InformationSubmitApproval selectByKey(String id) {
        return informationSubmitApprovalDao.selectByKey(id);
    }

    /*资料报审详情页面*/
    @Override
    public InformationSubmitApprovalDto queryDetailById(String id) {
        InformationSubmitApprovalDto dto = new InformationSubmitApprovalDto();
        //根据ID查询到资料预审(主表)的数据
        InformationSubmitApproval approval = informationSubmitApprovalDao.selectByKey(id);
        dto.setProName(approval.getProName());
        dto.setProCode(approval.getProCode());
        //查询融资报审的list有多少条
        int i = informationSubmitApprovalDao.sumByProCode(approval.getProCode());
        if (i <= 0) return dto;
        List<FinancingApproval> financingApprovalList = new ArrayList<FinancingApproval>();
        List<SetupFinancial> setupList = new ArrayList<>();
        //有融资报审数据
        for (int j = 0; j < i; j++) {
            //查询融资ID
            FinancingApproval financingApproval = informationSubmitApprovalDao.financeByProCode(approval.getProCode(), String.valueOf(j + 1));
            /*根据融资ID去金融表查询数据*/
            //根据融资ID取中间表取数据  然后拿到ID 取查询金融数据
            List<Map<String,String>> mapList = informationSubmitApprovalDao.findByMiddleList(financingApproval.getId());
            for (Map<String, String> map : mapList) {
                /*根据金融ID 查询金融数据*/
                SetupFinancial setup = informationSubmitApprovalDao.findByJinRoSGUID(map.get("SETUP_FINANCIAL_ID"));
                setupList.add(setup);
            }

            //遍历金融数据 查询出报审资料数据
            for (int l = 0; l < setupList.size(); l++) {
                //查询金融ID
                String JinRoId = setupList.get(l).getId();
                if (JinRoId==null || JinRoId != ""){
                    //查询报审资料数据
                    List<ReportMaterial> reportMaterialList = informationSubmitApprovalDao.findByJinRoId(JinRoId);
                    //填充数据
                    setupList.get(l).setReportMaterialList(reportMaterialList);
                }
            }
            //存放金融数据
            financingApproval.setSetupFinancialList(setupList);
            //根据processId流程ID查询并且返回审批意见
            List<ProcessNode> processNodeList = informationSubmitApprovalDao.selectProcessNodeByProcessId(financingApproval.getProcessId());
            financingApproval.setProcessNodeList(processNodeList);
            //存放到融资集合
            financingApprovalList.add(financingApproval);
        }
        dto.setFinancingApprovalList(financingApprovalList);
        return dto;
    }

    //资料报审详情金融机构下拉框数据
    @Override
    public List<Map<String, String>> queryFinancialDetail(String proCode) {
        List<Map<String, String>> list = informationSubmitApprovalDao.queryFinancialDetail(proCode);
        return list;
    }


    //资料报审详情 保存提交 前端传一个融资的数据过来
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(FinancingApproval financingApproval, String proCode) {
        /*根据融资 proCode和name查看是新增还是修改*/
        String name = financingApproval.getName();
        int i = informationSubmitApprovalDao.findByNameAndProCode(name, proCode);
        List<SetupFinancial> setupList = new ArrayList<>();
        if (i > 0) {//修改
            AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
            financingApproval.setUptBy(vo.getId());
            financingApproval.setUptTime(new Date());
            /*先删除原来的融资X下面的金融和报审数据*/
            // 根据融资名字和项目proCode查询之前的融资数据
            FinancingApproval finance = informationSubmitApprovalDao.financeByProCode(proCode, name);
            //查询出金融数据 根据金融数据的ID删除报审资料
            List<Map<String,String>> mapList = informationSubmitApprovalDao.findByMiddleList(finance.getId());
            for (Map<String, String> map : mapList) {
                /*根据金融ID 查询金融数据*/
                SetupFinancial setup = informationSubmitApprovalDao.findByJinRoSGUID(map.get("SETUP_FINANCIAL_ID"));
                setupList.add(setup);
            }
            for (int j = 0; j < setupList.size(); j++) {
                //查询金融ID
                String jinRoId = setupList.get(j).getId();
                //根据金融ID删除报审资料
                informationSubmitApprovalDao.deleteReportById(jinRoId);
            }
            //最后再删除之前中间表关联的融资ID和金融ID
            informationSubmitApprovalDao.deleteMiddleByRoZhiId(finance.getId());

            //把前端传递的融资ID和金融ID 保存到中间表
            saveRoZhiIdByJinRoId(financingApproval);
            //保存报审数据
           saveFinanceReport(vo, financingApproval);
        } else {//保存
            /*保存融资表的数据*/
            //登录人员信息
            AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
            financingApproval.setAddBy(vo.getId());
            financingApproval.setAddTime(new Date());
            informationSubmitApprovalDao.addFinancingApproval(financingApproval);
            //把前端传递的融资ID和金融ID 保存到中间表
            saveRoZhiIdByJinRoId(financingApproval);
            //保存报审数据
            saveFinanceReport(vo, financingApproval);
        }
        return 1;
    }

    public void saveRoZhiIdByJinRoId(FinancingApproval financingApproval) {
        //根据前端传递的金融ID /*把数据保存到融资和金融的中间表*/
        List<SetupFinancial> setupFinancials = financingApproval.getSetupFinancialList();
        for (SetupFinancial setupFinancial : setupFinancials) {
            //金融ID
            String setupFinancialId = setupFinancial.getId();
            /*把数据保存到融资和金融的中间表*/
            informationSubmitApprovalDao.insertByMiddleWrite(financingApproval.getId(), setupFinancialId);
        }
    }

    public void saveFinanceReport(AuthorizationVO vo, FinancingApproval financingApproval) {            //保存金融list 往金融表填充数据
        List<SetupFinancial> setupFinancialList = financingApproval.getSetupFinancialList();
        for (SetupFinancial setupFinancial : setupFinancialList) {
            /*取金融里面的报审数据*/
            List<ReportMaterial> materialList = setupFinancial.getReportMaterialList();
            for (ReportMaterial reportMaterial : materialList) {
                reportMaterial.setAddBy(vo.getId());
                reportMaterial.setAddTime(new Date());
                reportMaterial.setCapSetupFinancialId(setupFinancial.getId());
                //保存报审数据
                informationSubmitApprovalDao.addReportMaterial(reportMaterial);
            }
        }
    }

    /*资料报审金融机构下拉框选择事件接口*/
    @Override
    public SetupFinancial querySetupFinancialDetail(String id,String proCode) {
        return informationSubmitApprovalDao.querySetupFinancialDetail(id,proCode);
    }
    /*资料报审的融资报审数据审批通过后往融资批复主表写入数据*/
    @Override
    public ArrayList<String> saveFundingApproval(String proCode ,AuthorizationVO vo ) {
        ArrayList<String> list = new ArrayList<>();
        FinancingApproval financingApproval = informationSubmitApprovalDao.financeByProCodeMaxName(proCode);

        List<SetupFinancial> setupList = new ArrayList<>();
        //根据融资ID取中间表取数据  然后拿到ID 取查询金融数据
        List<Map<String,String>> mapList = informationSubmitApprovalDao.findByMiddleList(financingApproval.getId());
        for (Map<String, String> map : mapList) {
            /*根据金融ID 查询金融数据*/
            SetupFinancial setup = informationSubmitApprovalDao.findByJinRoSGUID(map.get("SETUP_FINANCIAL_ID"));
            setupList.add(setup);
        }
        //遍历金融数据 把数据保存到融资批复主表
        for (SetupFinancial setupFinancial : setupList) {
            FundingApproval fa = new FundingApproval();
            fa.setProName(financingApproval.getProName());
            fa.setProCode(proCode);
            fa.setAddBy(vo.getId());
            fa.setAddTime(new Date());
            fa.setProcessStatus("0");
            fa.setSetupFinancialId(setupFinancial.getId());
            fundingApprovalDao.insertFundingApproval(fa);
            String id = fa.getId();
            list.add(id);
        }
        return list;
    }
}

